<%--
  Created by IntelliJ IDEA.
  User: 李志文
  Date: 2024/4/10
  Time: 15:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>分页视图</title>

    <%--引入jquery文件--%>
    <script src="/js/jquery-3.6.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("input[name=del]").change(function () {
                var bo = this.checked;
                $("input[name=stu]").each(function () {
                    this.checked = bo;
                })
            });
            $("input[name=stu]").change(function () {
                var num = 0;
                $("input[name=stu]").each(function () {
                    if (this.checked == false) {
                        num++;
                    }
                });
                //判断num是否为零
                if (num > 0) {
                    document.querySelector("input[type=checkbox]").checked = false;
                } else {
                    document.querySelector("input[type=checkbox]").checked = true;
                }

            });
            $("#del").click(function () {
                //声明一个变量:用来保存要删除的id
                var ids = "";
                $("input[name=stu]").each(function () {
                    if (this.checked) {
                        ids += $(this).val() + ",";
                    }
                })
                ids = ids.substring(0, ids.length-1);
                if (ids.length > 0) {
                    location.href ="/StudentController/delStus?ids="+ids;
                } else {
                    alert("尚未选中选项！！！");
                }
            });
            if(${bo=='trueadd'}){
                alert("添加成功!!!!!");
            }else if(${bo=='truedel'}){
                alert("删除成功!!!!!");
            }else if(${bo=='falseadd'}){
                alert("添加失败!!!!!");
            }else if(${bo=='falsedel'}){
                alert("删除失败!!!!!");
            }else if(${bo=='trueedit'}){
                alert("编辑成功!!!!!");
            }else if(${bo=='falseedite'}){
                alert("编辑失败!!!!!");}
        })
        //编辑函数
        function editStu(stuNo,stuName){
            var bo=confirm("是否确认编辑学号为"+stuNo+"姓名为"+stuName+"的学生?")
            if(bo==true){
                //确认编辑，提交信息到服务器
                location.href="/StudentController/getStuByNo/"+stuNo+"?pageNow=${page.pageNow}&stuNo=${map.stuNo}&stuName=${map.stuName}&cid=${map.cid}";
            }
        }
        //删除函数
        function delStu(stuNo,stuName){
            var bo=confirm("是否确认删除学号为"+stuNo+"姓名为"+stuName+"的学生?")
            if(bo==true){
                //确认删除，提交信息到服务器
                location.href="/StudentController/delStu/"+stuNo+"?pageNow=${page.pageNow}&stuNo=${map.stuNo}&stuName=${map.stuName}&cid=${map.cid}";
            }
        }
    </script>
    <script type="text/javascript">
        $(function () {
            getAllCla();
        })
        //获取全部页面
        function getAllCla() {
            $.ajax({
                type:"post",//提交方式
                url:"/ClaController/getAllCla",//提交路径
                // data:"",//参数
                // dataType:"json",//接收数据类型(json 通过键获取值)
                // async:"",//是否异步
                success:function (data) {
                    var cid = $("#cid").val();
                    var option = null;
                    //遍历json数组:
                    $(data).each(function (){
                        if(cid != null && this.cid == cid){
                            option = $("<option value='"+this.cid+"' selected>"+this.cname+"</option>");
                        }else{
                            option = $("<option value='"+this.cid+"'>"+this.cname+"</option>");
                        }
                        $("select").append($(option));
                    });
                },
                error:function () {
                    //失败的回调函数
                }
            })
        }
    </script>
    <script type="text/javascript">
         //定义首页功能
         function firstPage() {
             //判断是不是第一页
            if(${page.pageNow==1}){
                alert("当前已经是首页")
            }else {
                location.href="/StudentController/getStuPage/1/add.html?stuNo=${map.stuNo}&stuName=${map.stuName}&cid=${map.cid}"
            }
         }
         //定义下一页的功能
        function nextPage() {
         if(${page.pageNow==page.pageSum}){
             alert("当前页已经是最后一页")
         }else {
             location.href="/StudentController/getStuPage/${page.pageNow+1}/add.html?stuNo=${map.stuNo}&stuName=${map.stuName}&cid=${map.cid}"
         }
        }
        //定义上一页的功能
        function upPage() {
            if(${page.pageNow==1}){
                alert("当前页已经是第一页")
            }else {
                location.href="/StudentController/getStuPage/${page.pageNow-1}/add.html?stuNo=${map.stuNo}&stuName=${map.stuName}&cid=${map.cid}"
            }
        }
        //定义尾页的功能
        function lastPage() {
            if(${page.pageNow==page.pageSum}){
                alert("当前页已经是最后一页")
            }else {
                location.href="/StudentController/getStuPage/${page.pageSum}/add.html?stuNo=${map.stuNo}&stuName=${map.stuName}&cid=${map.cid}"
            }
        }
        //定义一个跳转的方法
        function go() {
       //获取文本框内容
            var val=$("#go").val();
            //判断val是否符合规则
            if(isNaN(val)){
                alert("页码输入有误")
                $("#go").val("");
         }else{
                if(val>0&&val<${page.pageSum}){
                    location.href="/StudentController/getStuPage/"+val+"/add.html?stuNo=${map.stuNo}&stuName=${map.stuName}&cid=${map.cid}";
                }else{
                    alert("该页码不存在")
                    $("#go").val("");
                }
            }
        }
     </script>

</head>
<body>
<fieldset>
    <legend>登录成功！！！ <img src="/images/${uimage}" width="70px" height="70px" style="border-radius: 50%"/></legend>
    欢迎${uname}登录
    <form action="/StudentController/getStuPage/1/asa" method="get">
        <label for="stuNo">学号：</label><input type="text" style="width: 110px" name="stuNo" id="stuNo" placeholder="请输入学号" value="${map.stuNo}"/>
        <label for="stuName">姓名：</label><input type="text" style="width: 110px" name="stuName" id="stuName" placeholder="请输入姓名" value="${map.stuName}"/>
        <label>班级：</label>
        <select name="cid">
            <option value="-1">---请选择要查询的班级---</option>
            <input type="hidden" id="cid" value="${map.cid}"/>
        </select>
    <button>搜索</button>
    </form>
    <table>
        <p>
            <input type="button" value="删除已选" id="del">
            <input type="button" value="录入信息" onclick="location.href='/addStu.jsp'">
            <input type="button" value="全查页面" onclick="location.href='/StudentController/getAllStu/aa.html'">
        </p>
        <tr>
            <th>
                <input type="checkbox" name="del"/>
            </th>
            <th>学号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>班级</th>
            <th>简介</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${page.list}" var="stu">
            <tr>
                <td>
                    <input type="checkbox" name="stu" value="${stu.stuNo}"/>
                </td>
                <td>${stu.stuNo}</td>
                <td>${stu.stuName}</td>
                <td>${stu.stuAge}</td>
                <td>${stu.cla.cname}</td>
                <td>${stu.cla.content}</td>
                <td>
                    <a href="javascript:void(0)" onclick="delStu(${stu.stuNo},'${stu.stuName}')">删除</a>
                    <a href="javascript:void(0)" id="edit" onclick="editStu(${stu.stuNo},'${stu.stuName}')">编辑</a>
                </td>
            </tr>
        </c:forEach>
        <tr align="center">
        <td colspan="7">
            <button onclick="firstPage()">首页</button>&nbsp;
            <button onclick="upPage()">上一页</button>&nbsp;
            ${page.pageNow}/${page.pageSum}&nbsp;
            <button onclick="nextPage()">下一页</button>&nbsp;
            <button onclick="lastPage()">尾页</button>&nbsp;
            <input type="text" id="go" style="width:30px">&nbsp;<a href="javascript:go()">GO</a></input>
        </td>
        </tr>
    </table>
</fieldset>
</body>
</html>
